from time import sleep
from random import randint
from tkinter import ttk, scrolledtext
from tkinter import *
from ModeType.Base import BaseOutput

# 过山车式输出


class RollerCoasterResult(BaseOutput):
    def createConfig(self, root):
        self.commonConfig(root)
        box = ttk.LabelFrame(root, text='过山车输出配置')
        box.pack(padx=10, pady=10)

        row = 0
        ttk.Label(box, text='内容').grid(row=row, column=0)
        self.strtemp = scrolledtext.ScrolledText(box, height=10)
        self.strtemp.grid(row=row, column=1, sticky=W+E+N+S)
        self.strtemp.insert(INSERT, '请输入内容')
        
        row += 1
        ttk.Label(box, text='输出次数').grid(row=row, column=0)
        self.count = ttk.Spinbox(box, from_=1, to=10000)
        self.count.grid(row=row, column=1, pady=4, sticky=W+E+N+S)
        self.count.set(10)
        
        row += 1
        ttk.Label(box, text='最小叠加量(波谷)').grid(row=row, column=0)
        self.min = ttk.Spinbox(box, from_=1, to=10000)
        self.min.grid(row=row, column=1, pady=4, sticky=W+E+N+S)
        self.min.set(2)

        row += 1
        ttk.Label(box, text='最大叠加量(波峰)').grid(row=row, column=0)
        self.max = ttk.Spinbox(box, from_=1, to=10000)
        self.max.grid(row=row, column=1, pady=4, sticky=W+E+N+S)
        self.max.set(10)
        return box

    # 整理输出内容
    def formatResultDoc(self):
        content = self.get_strtemp()
        strList = content.split(",")
        result = ''
        i = 0
        while i < self.docCount:
            ri = randint(0, len(strList) - 1)
            result += strList[ri]
            i += 1

        self.keyResultContent(result)

    def run(self):
        self.docCount = 1    # 文案叠加初始量
        index = 0
        toggle = True
        count = self.get_count()
        speed = self.get_speed()
        while index < count:
            if self.stop:
                break
            if self.docCount < int(self.min.get()):
                toggle = True
            if self.docCount > int(self.max.get()):
                toggle = False
            if toggle:
                self.docCount += 1
            else:
                self.docCount -= 1

            self.formatResultDoc()
            if index % 3 == 0:
                toggle = (randint(0, 10) < 5)
            index += 1
            sleep(speed)
